package cn.gok.util;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

/**
 * @ClassName TreeMap例子
 * @Description TODO
 * @Author HP
 * @Date 14:15 2022/7/11
 * @Version 1.0
 **/
class Animal implements Comparable<Animal> {
    private String name;
    private int feetCount;

    @Override
    public String toString() {
        return "Animal{" +
                "name='" + name + '\'' +
                ", feetCount=" + feetCount +
                '}';
    }

    public Animal(String name, int feetCount) {
        this.name = name;
        this.feetCount = feetCount;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getFeetCount() {
        return feetCount;
    }

    public void setFeetCount(int feetCount) {
        this.feetCount = feetCount;
    }


    @Override
    public int compareTo(Animal o) {
        if(this.getFeetCount()<o.getFeetCount()){
            return -1;
        }else if (this.getName().equals(o.getName())){
            return 0;
        }
        return  1;
    }
}
class MyAnimalComparable implements Comparator<Animal>{


    @Override
    public int compare(Animal o1, Animal o2) {
        if(o1.getFeetCount()<o2.getFeetCount()){
            return -1;
        }else if (o1.getName().equals(o2.getName())){
            return 0;
        }
        return  1;
    }
}

public class TreeMap例子 {
    public static void main(String[] args) {
        //默认排序规则();
        自定义排序();

    }

    private static void 自定义排序() {
        Map<Animal,String> map = new TreeMap<>(new MyAnimalComparable());
        map.put(new Animal("老虎",4),"asd");
        map.put(new Animal("鸡",2),"asd");
        System.out.println(map);
    }

    private static void 默认排序规则() {
        Map<Integer,String > map = new TreeMap<>();
        map.put(10,"aaa");
        map.put(5,"bbb");
        map.put(8,"ccc");
        System.out.println(map);

        Map<String,String > map1 = new TreeMap<>();
        map1.put("a","aaa");
        map1.put("b","bbb");
        map1.put("c","ccc");
        System.out.println(map1);

    }
}
